using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BoOnce.Components;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Collisions;
using BoOnce.Physic;

namespace BoOnce.GameLogic.Entities.Drawable.GFX
{
    class Particule : PhysicalScenery
    {
        #region Private Attributes
        private double _CreationTime = 0; //ms
        private float _Blend = 1.0f;
        private float _Scale = 0.0f;
        private float _Rot = 0.0f;
        private Texture2D _Texture;
        #endregion

        #region Properties
        public double CreationTime { get { return _CreationTime; } }
        #endregion

        #region Public Attributes
        public ParticulesConfig Config;
        #endregion

        #region Constructor
        public Particule(Texture2D texture, Vector2 position, float mass, double creationTime, ParticulesConfig config)
            : base(position, texture.Width, texture.Height, mass)
        {
            Config = config;
            Position = position;
            _Texture = texture;
            _CreationTime = creationTime;
            _Body.IsStatic = false;
            _Body.IgnoreGravity = true;
            _Body.Enabled = true;
            _Geom.CollisionEnabled = false;
        }
        #endregion

        public void Dispose()
        {
            _PhysicComponent.Remove(_Body, _Geom);
        }

        public void SetImpulse(Vector2 impulse)
        {
            _Body.ApplyForce(impulse);
        }

        #region DrawableEntity
        public override void QuickDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_Texture, Camera.ToScreenPosition(Position), null, new Color(new Vector4(1f, 1f, 1f, _Blend)), _Rot,
    new Vector2(Width / 2.0f, Height / 2.0f), _Scale, SpriteEffects.None, 0.0f);
        }

        public override void Update(GameTime gameTime)
        {
            double currentTime = gameTime.TotalGameTime.TotalMilliseconds;
            float coeff = (float)((currentTime - _CreationTime) / (Config.LifeDuration));
            _Scale = MathHelper.SmoothStep(Config.ScaleBegin, Config.ScaleEnd, coeff);
            _Rot = MathHelper.SmoothStep(Config.RotBegin, Config.RotEnd, coeff);
            _Blend = 1.0f - MathHelper.SmoothStep(Config.BlendBegin, Config.BlendEnd, coeff);
        }
        #endregion
    }
}